/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Electricitycontract;
import domain.Userdom;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

/**
 *
 * @author Alejandro
 */
public class ElectricityContractDao {
    EntityManager em;
    EntityTransaction tx;

    public ElectricityContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Electricitycontract getElectricityContract(Integer idElectricityContract) {
        Electricitycontract e = (Electricitycontract) em.createNativeQuery("select * from electricitycontract as n where n.idElectricityContract='" + idElectricityContract + "'", Electricitycontract.class).getSingleResult();
        return e;
    }

    public void saveElectricityContract(Electricitycontract e) {
        tx.begin();
        em.persist(e);
        tx.commit();
    }

    public void removeElectricityContract(Electricitycontract e) {
        tx.begin();
        em.remove(e);
        tx.commit();
    }

    public List<Electricitycontract> getElecContrList(Userdom user) {
        tx.begin();
        List list = em.createNativeQuery("select * from bankaccount b, contract c, electricitycontract e where b.userFK='"+user.getNick()+"' and c.bankFK=b.idBank and e.contractFK=c.idContract", Electricitycontract.class).getResultList();
        tx.commit();
        return list;
    }

    public void updateElectricityContract(Electricitycontract e) {
        tx.begin();
        em.createNativeQuery("update electricitycontract set company='"+e.getCompany()+"', contratedPower='"+e.getContratedPower()+"' where idElectricityContract='"+e.getIdElectricityContract()+"'").executeUpdate();
        tx.commit();
    }
}